package day21.pack7;


import lombok.SneakyThrows;

import java.lang.ref.WeakReference;
import java.util.concurrent.CountDownLatch;

public class Test3 {
    @SneakyThrows
    public static void aa(String[] args) {
        Counter counter = new Counter();
        ThreadLocal<Integer> threadLocal = new ThreadLocal<>();

        CountDownLatch countDownLatch = new CountDownLatch(2);
        Runnable r = () -> {
            for (int i = 0; i < 100000; i++) {
                Integer count = threadLocal.get();
                if (count == null) {
                    threadLocal.set(counter.count + 1);
                } else {
                    threadLocal.set(count + 1);
                }
            }
            synchronized (counter) {
                counter.count += threadLocal.get();
            }
            countDownLatch.countDown();
        };
        new Thread(r, "A").start();
        new Thread(r, "B").start();
        countDownLatch.await();
        System.out.println(counter.count);
    }
}
